﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVM.Northwind.Employee.Services
{
    public class ServiceContainer
    {
        public static readonly ServiceContainer Instance = new ServiceContainer();

        readonly Dictionary<Type, object> _serviceMap;
        object _serviceMapLock;

        private ServiceContainer()
        {
            _serviceMap = new Dictionary<Type, object>();
            _serviceMapLock = new object();
        }

        public void AddService<TServiceContract>(TServiceContract Implementation)
            where TServiceContract : class
        {
            lock (_serviceMapLock)
            {
                _serviceMap[typeof(TServiceContract)] = Implementation;
            }
        }

        public TServiceContract GetService<TServiceContract>()
              where TServiceContract : class
        {
            object service;
            lock (_serviceMapLock)
            {
                _serviceMap.TryGetValue(typeof(TServiceContract), out service);
            }
            return service as TServiceContract;
        }

        
    }
}
